package com.zahb.qadx.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.model.ArchivesBean;
import com.zahb.qadx.model.NationBean;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.view.CircleImageView;
import com.zahb.qadx.util.ImageLoaderKt;
import com.zahb.qadx.util.Tips;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MyArchivesActivity extends BaseActivity {


    @BindView(R.id.rlIdcard)
    RelativeLayout rlIdcard;
    @BindView(R.id.rlWorkEx)
    RelativeLayout rlWorkEx;
    @BindView(R.id.rlTrainRecord)
    RelativeLayout rlTrainRecord;
    @BindView(R.id.rlStudyRecord)
    RelativeLayout rlStudyRecord;
    @BindView(R.id.rlAssessmentRecord)
    RelativeLayout rlAssessmentRecord;
    @BindView(R.id.rlPromotion)
    RelativeLayout rlPromotion;
    @BindView(R.id.rlBreakRule)
    RelativeLayout rlBreakRule;
    @BindView(R.id.rlOther)
    RelativeLayout rlOther;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvPhone)
    TextView tvPhone;
    @BindView(R.id.ivHead)
    CircleImageView ivHead;
    @BindView(R.id.tvCompany)
    TextView tvCompany;
    @BindView(R.id.tvArchivesNum)
    TextView tvArchivesNum;
    @BindView(R.id.tvEducation)
    TextView tvEducation;
    @BindView(R.id.tvNation)
    TextView tvNation;
    @BindView(R.id.tvCompanyTwo)
    TextView tvCompanyTwo;
    @BindView(R.id.tvDepartment)
    TextView tvDepartment;
    @BindView(R.id.tvIdcard)
    TextView tvIdcard;
    @BindView(R.id.tvBirthday)
    TextView tvBirthday;
    @BindView(R.id.tvJob)
    TextView tvJob;
    @BindView(R.id.tvProfessional)
    TextView tvProfessional;
    @BindView(R.id.tvSex)
    TextView tvSex;
    @BindView(R.id.tvPolitical)
    TextView tvPolitical;

    ArchivesBean archivesBean;
    @BindView(R.id.rlEducation)
    RelativeLayout rlEducation;


    public static ArchivesBean archive;
    @BindView(R.id.rlNation)
    RelativeLayout rlNation;
    @BindView(R.id.rlBirthday)
    RelativeLayout rlBirthday;
    @BindView(R.id.rlCompany)
    RelativeLayout rlCompany;
    @BindView(R.id.rlJob)
    RelativeLayout rlJob;
    @BindView(R.id.rlProfessional)
    RelativeLayout rlProfessional;
    @BindView(R.id.rlPolitical)
    RelativeLayout rlPolitical;
    @BindView(R.id.llCertificate)
    LinearLayout llCertificate;
    @BindView(R.id.rlEducationTwo)
    RelativeLayout rlEducationTwo;


    @Override
    protected int getLayout() {
        return R.layout.activity_my_archives;
    }

    @Override
    protected void initViews() {
        mUnbinder = ButterKnife.bind(this);

        rlIdcard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyArchivesActivity.this, UploadCertificateActivity.class);
                intent.putExtra("type", 2);
                if (archivesBean != null) {
                    intent.putExtra("data", archivesBean.getIdcardVO().getIdCardFrontImg());
                    intent.putExtra("datatwo", archivesBean.getIdcardVO().getIdCardBackImg());
                } else {
                    intent.putExtra("data", "");
                    intent.putExtra("datatwo", "");
                }
                startActivity(intent);
            }
        });

        rlEducationTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyArchivesActivity.this, UploadCertificateActivity.class);
                intent.putExtra("type", 3);
                if (archivesBean != null) {
                    intent.putExtra("data", archivesBean.getEducationImgVO().getEducationImg());
                } else {
                    intent.putExtra("data", "");
                }
                startActivity(intent);
            }
        });

        rlWorkEx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyArchivesActivity.this, WorkExperienceActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
            }
        });

        rlTrainRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyArchivesActivity.this, ArchivesRecordActivity.class);
                intent.putExtra("type", 2);
                startActivity(intent);
            }
        });

        rlStudyRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyArchivesActivity.this, ArchivesRecordActivity.class);
                intent.putExtra("type", 3);
                startActivity(intent);
            }
        });

        rlAssessmentRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyArchivesActivity.this, ArchivesRecordActivity.class);
                intent.putExtra("type", 4);
                startActivity(intent);
            }
        });


        addDisposable(RetrofitService.getNetService()
                .archives()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == 200) {
                        archivesBean = response.getData();
                        if (response.getData().getPerfectStatus() == 0) {
                            Intent intent = new Intent(this, PerfectArchivyActivity.class);

                            //PerfectArchivyActivity   UploadCertificateActivity  WorkExperienceActivity
                            if (response.getData().getInfoVO() != null && !TextUtils.isEmpty(response.getData().getInfoVO().getName())) {
                                intent.putExtra("name", response.getData().getInfoVO().getName());
                            } else {
                                intent.putExtra("name", "");
                            }
                            if (response.getData().getInfoVO() != null && !TextUtils.isEmpty(response.getData().getInfoVO().getPhone())) {
                                intent.putExtra("phone", response.getData().getInfoVO().getPhone());
                            } else {
                                intent.putExtra("phone", "");
                            }
                            startActivity(intent);
                            return;
                            //finish();
                        } else {
                            if (response.getData().getConfigStatus() == 0) {
                                //ToastUtils.s(this, "企业未配置档案模板，暂时无法查看档案");
                                showBindToast("企业未配置档案模板，暂时无法查看档案");
                                return;
                                //finish();
                            }
                        }
                        if (response.getData().getInfoVO() == null) {
                            return;
                        }

                        ImageLoaderKt.loadImageCircle(ivHead, response.getData().getInfoVO().getHeadImg());
                        if (!TextUtils.isEmpty(response.getData().getInfoVO().getName()))
                            tvName.setText(response.getData().getInfoVO().getName());
                        if (!TextUtils.isEmpty(response.getData().getInfoVO().getPhone()))
                            tvPhone.setText(response.getData().getInfoVO().getPhone());
                        if (!TextUtils.isEmpty(response.getData().getInfoVO().getIdCard()))
                            tvIdcard.setText(response.getData().getInfoVO().getIdCard());
                        tvSex.setText("1".equals(response.getData().getInfoVO().getGender()) ? "男" : "女");


                        ivHead.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(MyArchivesActivity.this, ImagePreviewActivity.class);
                                intent.putExtra("url", response.getData().getInfoVO().getHeadImg());
                                startActivity(intent);
                            }
                        });


                        if(isEmpty(response.getData().getInfoVO().getCompany())){
                            tvCompany.setVisibility(View.GONE);
                            rlCompany.setVisibility(View.GONE);
                        }else{
                            tvCompany.setText(response.getData().getInfoVO().getCompany());
                            tvCompanyTwo.setText(response.getData().getInfoVO().getCompany());
                        }

                        if(isEmpty(response.getData().getInfoVO().getNation())){
                            rlNation.setVisibility(View.GONE);
                        }else{
                            Gson gson = new Gson();
                            NationBean nationBean = gson.fromJson(getResources().getString(R.string.nation), NationBean.class);
                            if(TextUtils.isEmpty(response.getData().getInfoVO().getNation())){
                                tvNation.setText("");
                            }else{
                                int p = Integer.parseInt(response.getData().getInfoVO().getNation());
                                if(p==0){
                                    tvNation.setText("");
                                }else{
                                    tvNation.setText(nationBean.getNation().get(p-1).getText());
                                }
                            }


                        }


                        if(isEmpty(response.getData().getInfoVO().getBirthday())){
                            rlBirthday.setVisibility(View.GONE);
                        } else {
                            tvBirthday.setText(response.getData().getInfoVO().getBirthday());
                        }


                        if (isEmpty(response.getData().getInfoVO().getEducation())) {
                            rlEducation.setVisibility(View.GONE);
                        } else {

                            if(TextUtils.isEmpty(response.getData().getInfoVO().getEducation())){
                                tvEducation.setText("");
                            }else{
                                tvEducation.setText(getEducation(Integer.parseInt(response.getData().getInfoVO().getEducation()) - 1));

                            }
                        }

                        if (isEmpty(response.getData().getInfoVO().getProfessional())) {
                            rlProfessional.setVisibility(View.GONE);
                        } else {
                            tvProfessional.setText(response.getData().getInfoVO().getProfessional());
                        }

                        if (isEmpty(response.getData().getInfoVO().getJob())) {
                            rlJob.setVisibility(View.GONE);
                        } else {
                            tvJob.setText(response.getData().getInfoVO().getJob());
                        }

                        if (isEmpty(response.getData().getInfoVO().getPoliticalOutlook())) {
                            rlPolitical.setVisibility(View.GONE);
                        } else {
                            if(TextUtils.isEmpty(response.getData().getInfoVO().getPoliticalOutlook())){
                                tvPolitical.setText("");
                            }else{
                                tvPolitical.setText(getPolitical(Integer.parseInt(response.getData().getInfoVO().getPoliticalOutlook()) - 1));
                            }
                        }

                        if (isEmpty(response.getData().getCode())) {
                            llCertificate.setVisibility(View.GONE);
                        } else {
                            tvArchivesNum.setText(response.getData().getCode());
                        }


                        for (ArchivesBean.ModularListEntity modularListEntity : response.getData().getModularList()) {
                            if ("工作经历".equals(modularListEntity.getName())) {
                                rlWorkEx.setVisibility(View.VISIBLE);
                            }
                            if ("培训记录".equals(modularListEntity.getName())) {
                                rlTrainRecord.setVisibility(View.VISIBLE);
                            }
                            if ("学习记录".equals(modularListEntity.getName())) {
                                rlStudyRecord.setVisibility(View.VISIBLE);
                            }
                            if ("考核记录".equals(modularListEntity.getName())) {
                                rlAssessmentRecord.setVisibility(View.VISIBLE);
                            }
                            if ("晋升记录".equals(modularListEntity.getName())) {
                                rlPromotion.setVisibility(View.VISIBLE);
                            }
                        }
                    } else {
                        showBindToast(response.getErrorInfo());
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    Tips.RequestError(getActivity());

                }));
    }


    @Override
    protected int getTitleStringRes() {
        return R.string.MyArchives;
    }

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, MyArchivesActivity.class);
        context.startActivity(intent);
    }


    public String getEducation(int index) {
        if(index<0){
            return "";
        }
        List<String> list = new ArrayList<>();

        list.add("小学");
        list.add("初中");
        list.add("高中");
        list.add("技工学校");
        list.add("中专或中技");
        list.add("专科");
        list.add("本科");
        list.add("研究生");
        list.add("其它");

        if (index >= list.size()) {
            return list.get(list.size() - 1);
        }

        return list.get(index);
    }

    public String getPolitical(int index) {
        if(index<0){
            return "";
        }
        List<String> list = new ArrayList<>();

        list.add("中共党员");
        list.add("中共预备党员");
        list.add("共青团员");
        list.add("民革党员");
        list.add("民盟盟员");
        list.add("民建会员");
        list.add("民进会员");
        list.add("农工党党员");
        list.add("致公党党员");
        list.add("九三学社社员");
        list.add("台盟盟员");
        list.add("无党派人士");
        list.add("群众");

        if (index >= list.size()) {
            return list.get(list.size() - 1);
        }

        return list.get(index);
    }

    public boolean isEmpty(String s){
/*        if(TextUtils.isEmpty(s)){
            return true;
        }*/
        if("noDisplay".equals(s)){
            return true;
        }
        return false;
    }


}
